﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listsOutput : Api_BaseOutput
   {
      public List<listsResults> result { get; set; }
      public listsParms inputParms { get; private set; }
      //
      public listsOutput( listsInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new List<listsResults>();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "apikey: {0}, number of lists: {1}",
            this.inputParms.apikey, result.Count );
      }

      #region ================================================================== format setup
      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }
      // ---- xmlrpc
      internal void Format( x_list[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }
      #endregion

      #region  ===================================================================== format result
      // ---------------------------------------------------------- outputXML
      protected void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            int i; DateTime dt; double dbl;
            IEnumerable<XElement> entries = xml.Elements( "struct" );
            foreach ( XElement entry in entries )
            {
               listsResults results = new listsResults();
               results.id = entry.Element( "id" ).Value;
               int.TryParse( entry.Element( "web_id" ).Value, out i );
               results.web_id = i;
               results.name = entry.Element( "name" ).Value;
               DateTime.TryParse( entry.Element( "date_created" ).Value, out dt );
               results.date_created = dt;
               int.TryParse( entry.Element( "member_count" ).Value, out i );
               results.member_count = i;
               int.TryParse( entry.Element( "unsubscribe_count" ).Value, out i );
               results.unsubscribe_count = i;
               int.TryParse( entry.Element( "cleaned_count" ).Value, out i );
               results.cleaned_count = i;
               // v1.2.2
               results.email_type_option = ( entry.Element( "email_type_option" ).Value.Trim() == "1" ) ? true : false;
               results.default_from_name = entry.Element( "default_from_name" ).Value;
               results.default_from_email = entry.Element( "default_from_email" ).Value;
               results.default_subject = entry.Element( "default_subject" ).Value;
               results.default_language = entry.Element( "default_language" ).Value;
               // v1.2.5
               double.TryParse( entry.Element( "list_rating" ).Value, out dbl );
               results.list_rating = dbl;
               int.TryParse( entry.Element( "member_count_since_send" ).Value, out i );
               results.member_count_since_send = i;
               int.TryParse( entry.Element( "unsubscribe_count_since_send" ).Value, out i );
               results.unsubscribe_count_since_send = i;
               int.TryParse( entry.Element( "cleaned_count_since_send" ).Value, out i );
               results.cleaned_count_since_send = i;
               //
               this.result.Add( results );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- outputJSON
      protected void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_list[] jsonResult = jss.Deserialize<x_list[]>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // -----------------------------------------------------
      protected List<listsResults> FormatResults( x_list[] x_results )
      {
         List<listsResults> results = new List<listsResults>();

         int i; DateTime dt; double dbl;
         // array to generic list
         foreach ( x_list tlist in x_results )
         {
            listsResults list = new listsResults();

            list.id = tlist.id;
            list.web_id = tlist.web_id;
            list.name = tlist.name;
            DateTime.TryParse( tlist.date_created, out dt ); list.date_created = dt;
            int.TryParse( tlist.member_count.ToString(), out i ); list.member_count = i;
            int.TryParse( tlist.unsubscribe_count.ToString(), out i ); list.unsubscribe_count = i;
            int.TryParse( tlist.cleaned_count.ToString(), out i ); list.cleaned_count = i;
            // v1.2.2
            list.email_type_option = tlist.email_type_option;
            list.default_from_name = tlist.default_from_name;
            list.default_from_email = tlist.default_from_email;
            list.default_subject = tlist.default_subject;
            list.default_language = tlist.default_language;
            // v1.2.5
            double.TryParse( tlist.list_rating.ToString(), out dbl );
            list.list_rating = dbl;
            int.TryParse( tlist.member_count_since_send.ToString(), out i );
            list.member_count_since_send = i;
            int.TryParse( tlist.unsubscribe_count_since_send.ToString(), out i );
            list.unsubscribe_count_since_send = i;
            int.TryParse( tlist.cleaned_count_since_send.ToString(), out i );
            list.cleaned_count_since_send = i;
            //
            results.Add( list );
         }
         return results;
      }

      #endregion
   }
}
